﻿using System;
using System.Collections.Generic;
using System.Text;
using VioletDevelopmentLaboratory.Net.Web.Component;
using VioletDevelopmentLaboratory.Net.Web.Configuration;
using System.Web;
using System.Web.Security;

namespace VioletDevelopmentLaboratory.Net.Web
{
	public interface IVBoardUserInfoBridge
	{
		VBoardUserInfo GetUserInfo();
	}

	[Serializable]
	public class VBoardUserInfo
	{
		private String name;
		public String Name { get { return this.name; } private set { this.name = value; } }

		private String providerUserKey;
		public String ProviderUserKey { get { return this.providerUserKey; } private set { this.providerUserKey = value; } }

		private Boolean isAdministrator;
		public Boolean IsAdministrator { get { return isAdministrator; } private set { isAdministrator = value; } }

		public Boolean IsSysop(Board currentBoard)
		{
			if (HttpContext.Current.User.Identity.IsAuthenticated && (currentBoard.SysopCollection != null))
			{
				foreach (Sysop sysop in currentBoard.SysopCollection)
				{
					if (sysop.ProviderUserKey == ProviderUserKey)
					{
						return true;
					}
				}
			}
			return false;
		}

		public Boolean IsAuthorizedBy(Board currentBoard, BoardCommand authority)
		{
			if (currentBoard == null)
				throw new ArgumentNullException("currentBoard");

			Boolean _result = false;
			String _commandRole = "";

			switch (authority)
			{
				case BoardCommand.ListView:
					_commandRole = currentBoard.ListViewRoles;
					break;

                case BoardCommand.ThreadWrite:
					_commandRole = currentBoard.ThreadWriteRoles;
					break;

                case BoardCommand.ThreadReplyWrite:
					_commandRole = currentBoard.ThreadReplyWriteRoles;
					break;

                case BoardCommand.Read:
					_commandRole = currentBoard.ReadRoles;
					break;

                case BoardCommand.CommentWrite:
					_commandRole = currentBoard.CommentWriteRoles;
					break;

                case BoardCommand.AnnoWrite:
					_commandRole = currentBoard.AnnoWriteRoles;
					break;

                case BoardCommand.FileUpload:
					_commandRole = currentBoard.FileUploadRoles;
					break;

                case BoardCommand.FileDownload:
					_commandRole = currentBoard.FileDownloadRoles;
					break;
			}
			if (_commandRole.IndexOf(DefaultRoles.Guest) > -1)
			{
				return true;
			}
			if (HttpContext.Current.User.Identity.IsAuthenticated)
			{
				if (_commandRole.IndexOf(DefaultRoles.Authenticated) > -1)
				{
					_result = true;
				}
				if (Roles.Enabled)
				{
					foreach (String _userRole in Roles.GetRolesForUser())
					{
						if (_commandRole.IndexOf(_userRole) > -1)
						{
							_result = true;
						}
					}
				}
			}
			return _result;
		}

		private VBoardUserInfo() { }

		public VBoardUserInfo(String username, String providerUserKey)
		{
			if (username == null)
				throw new ArgumentNullException("username");

			if (providerUserKey == null)
				throw new ArgumentNullException("providerUserKey");

			this.Name = username;
			this.ProviderUserKey = providerUserKey;

			this.IsAdministrator = false;

			if (Roles.Enabled)
			{
				String administratorRoleName = VWebConfigurationReader.Administrator.Role;
				if (!String.IsNullOrEmpty(administratorRoleName))
				{
					foreach (String roleName in Roles.GetRolesForUser(username))
					{
						if (administratorRoleName == roleName)
						{
							this.IsAdministrator = true;
							break;
						}
					}
				}
			}

			String[] administratorNames = VWebConfigurationReader.Administrator.Names.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
			foreach (String name in administratorNames)
			{
				if (name == username)
				{
					this.IsAdministrator = true;
					break;
				}
			}
		}
	}
}
